<?php
/**
 * 
 * Author: wxl
 * Date: 2016/4/25
 * Time: 09:44
 * intro:充电宝充电 
 */
class AlipayService extends BaseService
{
    /**
     * 获取用户地理位置信息
     * $alipayId  支付宝账户id
     * $alipayUserId 支付宝用户id
     */
	public function getUserLocation($alipayId,$alipayUserId){
		$alipay = M('Alipay')->where(array('alipay_id'=>$alipayId))->field('appid,rsa_private_key,rsa_public_key')->limit(1)->find();
		$aluser = M('AlipayUser')->where(array('alipay_user_id'=>$alipayUserId))->field('original_alipay_user_id')->limit(1)->find();
		if($alipay && $aluser){
			vendor('Alipay.AopClient');
			vendor('Alipay.AlipayOpenPublicGisQueryRequest');
			$aop = new AopClient();
			$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
			$aop->appId = $alipay['appid'];
			$aop->rsaPrivateKeyFilePath = $alipay['rsa_private_key'];
			//$aop->alipayPublicKey= $alipay['rsa_public_key'];
			$aop->apiVersion = '1.0';
			$aop->postCharset = 'UTF-8';
			$aop->format = 'json';
			$request = new AlipayOpenPublicGisQueryRequest();
			$arr = array(
				'user_id'=>$aluser['original_alipay_user_id']
			);
			$request->setBizContent(json_encode($arr));
			$result = $aop->execute($request);
			$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
			$resultCode = $result->$responseNode->code;
			$arr = array(
				'code'=>$resultCode,
				'msg' => $result->$responseNode->msg,
				'accuracy' => $result->$responseNode->accuracy,
				'city' => $result->$responseNode->city,
				'latitude' => $result->$responseNode->latitude,
				'longitude' => $result->$responseNode->longitude,
				'province' => $result->$responseNode->province,
				'sign'=>$result->$responseNode->sign,
			);
			/*$res = $result->alipay_open_public_gis_query_response;

			//将返回的对象重组为需要的数组
			$arr = array(
				'code'=>$res->code,
				'msg' => $res->msg,
				'accuracy' => $res->accuracy,
				'city' => $res->city,
				'latitude' => $res->latitude,
				'longitude' => $res->longitude,
				'province' => $res->province,
				'sign'=>$res->sign,
			);*/
			//error_log(var_export($arr,true),3,'/tmp/alipaylocation.log');
			return $arr;

		}else{
			return false;
		}
	}

	/**
	 * 发送消息接口
	 * $alipayId  支付宝账户id
	 * $alipayUserId 支付宝用户id
	 */
	public function sendMessage($alipayId,$alipayUserId,$message,$tplId){
		$alipay = M('Alipay')->where(array('alipay_id'=>$alipayId))->field('appid,rsa_private_key,rsa_public_key')->limit(1)->find();
		$aluser = M('AlipayUser')->where(array('alipay_user_id'=>$alipayUserId))->field('original_alipay_user_id')->limit(1)->find();
		if($alipay && $aluser){
			vendor('Alipay.AopClient');
			vendor('Alipay.AlipayOpenPublicMessageSingleSendRequest');
			$aop = new AopClient();
			$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
			$aop->appId = $alipay['appid'];
			$aop->rsaPrivateKeyFilePath = $alipay['rsa_private_key'];
			//$aop->alipayPublicKey= $alipay['rsa_public_key'];
			$aop->apiVersion = '1.0';
			$aop->postCharset = 'UTF-8';
			$aop->format = 'json';
			$msgBody = array(
				'to_user_id' => $aluser['original_alipay_user_id'],
				'template'   =>array(
					'template_id' => $tplId,
					'context'     => $message
				)
			);

			$request = new AlipayOpenPublicMessageSingleSendRequest();
			$request->setBizContent(json_encode($msgBody));
			$result = $aop->execute($request);
			$responseNode = str_replace(".","_",$request->getApiMethodName())."_response";
			$resultCode   = $result->$responseNode->code;
			if(!empty($resultCode) && $resultCode == 10000){
				return $resultCode;
			}else{
				return $resultCode;
			}
		}
	}


	/**
	 * 支付宝网站支付
	 * $accountId 主帐号ID
	 * $body 支付主体信息（描述，名称，订单号等）
	 */
	public function alipayWapPay($accountId,$body,$returnUrl,$notifyUrl){
		$alipay = M('Alipay')->where(array('account_id'=>$accountId,''))->field('appid,rsa_private_key,rsa_public_key')->limit(1)->find();
		vendor('Alipay.AopClient');
		vendor('Alipay.request.AlipayTradeWapPayRequest');
		$aop = new AopClient();
		$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
		$aop->appId = $alipay['appid'];
		$aop->rsaPrivateKeyFilePath = $alipay['rsa_private_key'];
		//$aop->alipayPublicKey= $alipay['rsa_public_key'];
		$aop->apiVersion = '1.0';
		$aop->postCharset = 'UTF-8';
		$aop->format = 'json';
		$request = new AlipayTradeWapPayRequest();
		$request->setReturnUrl(urlencode($returnUrl));
		$request->setNotifyUrl(urlencode($notifyUrl));
		$request->setBizContent(json_encode($body));
		$result = $aop->pageExecute($request,'GET');
		return $result;

	}

	/**
	 * 记录到错误日志
	 */
	public function setErrorLog($data,$num){
		$json = json_encode($data);
		M('test_log')->add(
			array(
				'num'=>$num,
				'content' =>$json,
				'create_time' =>time(),
			)
		);
	}

}